package com.springboot.test.config;


import java.util.List;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

public class AutoConfigurationImportSelectorDemo implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {

        //获取EnableAutoDemoConfig所在的META-INF/spring.fctories里面的
        //EnableAutoDemoConfig类全名为key对应的值的Class全名
        List<String> configurations  = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), null);
        Assert.notEmpty(configurations,"No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return StringUtils.toStringArray(configurations);
    }

    /*----------------------------- 方法区 ------------------------------------------------*/
    private Class<?> getSpringFactoriesLoaderFactoryClass() {
        return EnableAutoDemoConfig.class;
    }

}
